package com.yyfax.common.form.adapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public abstract class BaseAdapter<T> extends android.widget.BaseAdapter {

	protected List<T> mDataSet;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public BaseAdapter() {
		mDataSet =  new ArrayList();
	}



	public void addData(T data) {
		if (data != null) {
			mDataSet.add(data);
		}
	}

	public void addData(Collection<T> data) {
		if (data != null) {
			mDataSet.addAll(data);
		}
	}

	public void addData(int index, Collection<T> data) {
		if (data != null) {
			mDataSet.addAll(index, data);
		}
	}

	public void removeData(Collection<T> data) {
		if (data != null) {
			mDataSet.removeAll(data);
		}
	}

	public void removeAll() {
		if (mDataSet != null) {
			mDataSet.clear();
		}
	}

	public void remove(T data) {
		if (data != null) {
			mDataSet.remove(data);
		}
	}

	public void remove(int position) {
		mDataSet.remove(position);
	}

	public List<T> subData(int index, int count) {
		return mDataSet.subList(index, index + count);
	}

	@Override
	public int getCount() {
        if (mDataSet != null) {
            return mDataSet.size();
        }
		return 0;
	}

	@Override
	public T getItem(int position) {
		return mDataSet.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0L;
	}

	public void setItem(int position, T obj) {
		mDataSet.set(position, obj);
	}

	public void addItem(int position, T obj) {
		mDataSet.add(position, obj);
	}

	public void addItems(int position, Collection<T> data) {
		mDataSet.addAll(position, data);
	}

	public List<T> getDataSet() {
		return mDataSet;
	}
}
